<?php 
	$NhanVien = new NhanVien();
	$resultNhanVien = json_encode($NhanVien->getDsNhanVien());
	$lfNhanVien = "	{name:'msNhanVien', type:'number'}, 
					{name:'hoTen', type:'string'},
					{name:'ngaySinh', type:'date'},
					{name:'queQuan', type:'string'},
					{name:'CMND', type:'string'},
					{name:'soDT', type:'string'},
					{name:'tenDangNhap', type:'string'},
					{name:'isAdmin', type:'boolean'}";
?>
<script>
var dtNhanVien = <?php echo $resultNhanVien; ?>;
var dsNhanVien = new Ext.data.Store({
	fields: [<?php echo $lfNhanVien; ?>],
	data: dtNhanVien
	});
	
dsNhanVien.load();



function resetField_frmInfoNV()
{
//	Ext.getCmp('txtMSNV').reset();
	Ext.getCmp('txtHoTenNV').reset();
	Ext.getCmp('dateNgaySinhNV').reset();
	Ext.getCmp('txtQueQuanNV').reset();
	Ext.getCmp('txtCMND').reset();
	Ext.getCmp('txtSoDTNV').reset();
	Ext.getCmp('txtTenDangNhap').reset();
	Ext.getCmp('txtNhapLaiMatKhau').reset();
	Ext.getCmp('txtMatKhau').reset();
	Ext.getCmp('cmbQuyenHan').reset();
	}
	
function disableField_frmInfoNV(value)
{
	if(value)
	{
		Ext.getCmp('txtHoTenNV').setDisabled(true);
		Ext.getCmp('txtQueQuanNV').setDisabled(true);
		Ext.getCmp('dateNgaySinhNV').setDisabled(true);
		Ext.getCmp('txtCMND').setDisabled(true);
		Ext.getCmp('txtSoDTNV').setDisabled(true);
		Ext.getCmp('txtTenDangNhap').setDisabled(true);
		Ext.getCmp('txtMatKhau').setDisabled(true);
		Ext.getCmp('txtNhapLaiMatKhau').setDisabled(true);
		Ext.getCmp('cmbQuyenHan').setDisabled(true);
	} 
	else {
		Ext.getCmp('txtHoTenNV').setDisabled(false);
		Ext.getCmp('txtQueQuanNV').setDisabled(false);
		Ext.getCmp('dateNgaySinhNV').setDisabled(false);
		Ext.getCmp('txtCMND').setDisabled(false);
		Ext.getCmp('txtSoDTNV').setDisabled(false);
		Ext.getCmp('txtTenDangNhap').setDisabled(false);
		Ext.getCmp('txtMatKhau').setDisabled(false);
		Ext.getCmp('txtNhapLaiMatKhau').setDisabled(false);
		Ext.getCmp('cmbQuyenHan').setDisabled(false);
	}
}
		
function bindDataToFrmInfoNV()
{
	var row =  Ext.getCmp('grdDSNhanVien_tabNhanVien').getSelectionModel().getSelection()[0]; 
	//Ext.getCmp('txtMSNV').setValue(row.get('msNhanVien'));
	Ext.getCmp('txtHoTenNV').setValue(row.get('hoTen'));
	Ext.getCmp('dateNgaySinhNV').setValue(row.get('ngaySinh'));
	Ext.getCmp('txtQueQuanNV').setValue(row.get('queQuan'));
	Ext.getCmp('txtCMND').setValue(row.get('CMND'));
	Ext.getCmp('txtSoDTNV').setValue(row.get('soDT'));
	Ext.getCmp('txtTenDangNhap').setValue(row.get('tenDangNhap'));
	
	Ext.getCmp('txtMatKhau').emptyText = '000000';
	Ext.getCmp('txtMatKhau').applyEmptyText();
	
	Ext.getCmp('txtNhapLaiMatKhau').emptyText = '000000';
	Ext.getCmp('txtNhapLaiMatKhau').applyEmptyText();
	
	if(row.get('isAdmin') == true)
		Ext.getCmp('cmbQuyenHan').select('Administrator');
	else
		Ext.getCmp('cmbQuyenHan').select('Nhân viên');
	}




// binding data from grid ds nhân viên sang panel chức năng
function _selectionRecordDSNV()
{
	if(Ext.getCmp('btnHuyNV').disabled)
	{
		
		disableField_frmInfoNV(true);
		bindDataToFrmInfoNV();
			
		Ext.getCmp('btnThemNV').setDisabled(false);
		Ext.getCmp('btnSuaNV').setDisabled(false);
		Ext.getCmp('btnHuyNV').setDisabled(true);
		Ext.getCmp('btnLuuNV').setDisabled(true);
	}
	else 	
		Ext.Msg.show(
		{
     		title:'Lưu thay đổi?',
     		msg: 'Dữ liệu bạn thay đổi sẽ bị xóa. Bạn có muốn lưu lại không?',
     		buttons: Ext.Msg.YESNOCANCEL,
     		icon: Ext.Msg.QUESTION,
			fn: function(btn)
			{
				switch(btn)
				{
					case "yes":
						 _clickBtnLuuNV();
						 break;
					case "no":
						 _clickBtnHuyNV();
						 bindDataToFrmInfoNV();
						 break;
					default:
					 	break;
				}
				
				}
		}
		);		
	};


// click button Thêm mới nhân viên
function _clickBtnThemNV()
{
	disableField_frmInfoNV(false);
	resetField_frmInfoNV();
	
	//Ext.getCmp('txtMSNV').setValue(dsNhanVien.getCount() + 1);
	Ext.getCmp('txtMatKhau').emptyText = [];
	Ext.getCmp('txtMatKhau').applyEmptyText();
	Ext.getCmp('txtNhapLaiMatKhau').emptyText = [];
	Ext.getCmp('txtNhapLaiMatKhau').applyEmptyText();
	
	Ext.getCmp('txtHoTenNV').focus();
	
	this.setDisabled(true);
	Ext.getCmp('btnSuaNV').setDisabled(true);
	Ext.getCmp('btnHuyNV').setDisabled(false);
	Ext.getCmp('btnLuuNV').setDisabled(false);
	}

// click button Sửa nhân viên
function _clickBtnSuaNV()
{
	disableField_frmInfoNV(false);
	Ext.getCmp('txtHoTenNV').focus();
	
	Ext.getCmp('btnSuaNV').setDisabled(true);
	Ext.getCmp('btnThemNV').setDisabled(true);
	Ext.getCmp('btnHuyNV').setDisabled(false);
	Ext.getCmp('btnLuuNV').setDisabled(false);
	}



// click button Lưu nhân viên
function _clickBtnLuuNV()
{
	/*Ext.getCmp('frmInfoNV_tabNhanVien').submit({
			params:{
				action:'Add',
				//id:employee_data.getAt(0).data.id
			}, 
			waitMsg:'Saving...'
		});*/
	Ext.getCmp('frmInfoNV_tabNhanVien').submit({
		url: 'http://localhost/CodeQuanLy/Controller/c_quanly.php',
                waitMsg: 'Loading...',
                method: 'POST',
		success: function () {     
                        Ext.MessageBox.alert ('Message','Data has been saved');
                
                    },
            failure: function () {   
                        Ext.MessageBox.alert ('Message','Saving data failed');
                    }
		});
		
	
	/*if(Ext.getCmp('frmInfoNV_tabNhanVien').isValid())
	{
	Ext.getCmp('frmInfoNV_tabNhanVien').submit(
	{
		//url:'http://localhost/CodeQuanLy/Controller/c_nhanvien.php',
                    waitMsg: 'Saving...',   
                    success: function () {     
                        Ext.MessageBox.alert ('Message','Data has been saved');
                
                    },
                  failure: function () {   
                        Ext.MessageBox.alert ('Message','Saving data failed');
                    }
	});
	}
	else
		Ext.Msg.alert('Errors', 'Please fix the errors noted.');*/
	//Ext.Msg.alert('Hế lô!', 'How 3 you?');
	/*Ext.getCmp('frmInfoNV_tabNhanVien').submit({
			params:{
				action:'submit',
				id: employee_data.getAt(0).data.id
			}, 
			waitMsg:'Saving...'
		});	
	}else{
		Ext.MessageBox.alert('Errors', 'Please fix the errors noted.');
	}*/
	/*disableField_frmInfoNV(true);
	resetField_frmInfoNV();
	
	Ext.getCmp('txtMatKhau').emptyText = [];
	Ext.getCmp('txtMatKhau').applyEmptyText();
	
	Ext.getCmp('txtNhapLaiMatKhau').emptyText = [];
	Ext.getCmp('txtNhapLaiMatKhau').applyEmptyText();

	Ext.getCmp('btnHuyNV').setDisabled(true);
	Ext.getCmp('btnThemNV').setDisabled(false);
	Ext.getCmp('btnSuaNV').setDisabled(true);
	Ext.getCmp('btnLuuNV').setDisabled(true);*/
	}



//click button Hủy bỏ
function _clickBtnHuyNV()
{
	disableField_frmInfoNV(true);
	resetField_frmInfoNV();
	
	Ext.getCmp('txtMatKhau').emptyText = [];
	Ext.getCmp('txtMatKhau').applyEmptyText();
	
	Ext.getCmp('txtNhapLaiMatKhau').emptyText = [];
	Ext.getCmp('txtNhapLaiMatKhau').applyEmptyText();

	Ext.getCmp('btnHuyNV').setDisabled(true);
	Ext.getCmp('btnThemNV').setDisabled(false);
	Ext.getCmp('btnSuaNV').setDisabled(true);
	Ext.getCmp('btnLuuNV').setDisabled(true);
	}


</script>